package com.example.shop_2211.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.base.mvvm.BaseFragment
import com.example.shop_2211.R
import com.example.shop_2211.adapter.GoodsAdapter
import com.example.shop_2211.adapter.OneAdapter
import com.example.shop_2211.adapter.TwoChildAdapter
import com.example.shop_2211.databinding.FragmentCateBinding
import com.example.shop_2211.fgViewModel.CategoryViewModel


class CateFragment : BaseFragment<FragmentCateBinding,CategoryViewModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoChildAdapter: TwoChildAdapter
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        twoChildAdapter = TwoChildAdapter()
        binding.rvTwo.adapter = twoChildAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(context,3)

        goodsAdapter = GoodsAdapter()
        binding.rvThree.adapter = goodsAdapter
        binding.rvThree.layoutManager = LinearLayoutManager(context)

        viewModel.category()
        //点击一级分类 二级数据变化  三级数据变化
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setId(oneAdapter.data[position].id)
            //二级数据变化
            twoChildAdapter.data.clear()
            twoChildAdapter.data.addAll(oneAdapter.data[position].child)
            twoChildAdapter.notifyDataSetChanged()
            //三级数据变化
            viewModel.search(twoChildAdapter.data[0].id,"",10,true,1,"","")
        }
        //点击二级分类，三级数据变化
        twoChildAdapter.setOnItemClickListener { adapter, view, position ->
            viewModel.search(twoChildAdapter.data[position].id,"",10,true,1,"","")
        }
        //左滑删除
        val itemTouchHelper = ItemTouchHelper(object :ItemTouchHelper.Callback(){
            override fun getMovementFlags(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder
            ): Int {
                val left = ItemTouchHelper.LEFT
                return makeMovementFlags(0,left)//返回左右滑动的标志
            }

            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                //滑动后执行结果
                val position = viewHolder.layoutPosition
                goodsAdapter.data.removeAt(position)
                goodsAdapter.notifyDataSetChanged()
            }
        }).attachToRecyclerView(binding.rvThree)

    }

    override fun initData() {
        viewModel.success.observe(this){
            if(it.code==200){
                //一级分类
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //二级分类 显示一级分类的第一个item中的child
                twoChildAdapter.data.clear()
                twoChildAdapter.data.addAll(it.data[0].child)
                twoChildAdapter.notifyDataSetChanged()
                //显示商品列表 //显示二级分类中第一个item的商品
                viewModel.search(twoChildAdapter.data[0].id,"",10,true,1,"","")
            }
        }
        viewModel.searchSuccess.observe(this){
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CategoryViewModel>  = CategoryViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_cate

}